package com.yzg.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yzg.pojo.Brand;
import com.yzg.service.BrandService;
import com.yzg.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: decade
 * @Description: 品牌管理
 * @Date: 2021/1/13 18:27
 **/
@Api(tags = "BrandController", description = "品牌管理")
@RestController
@RequestMapping("/pms/brand")
@CrossOrigin
public class BrandController {
    @Autowired
    private BrandService brandService;

    @GetMapping("/getList/{page}/{pageSize}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "当前页",required = true,paramType = "Integer"),
            @ApiImplicitParam(name = "pageSize",value = "当前页大小",required = true,paramType = "Integer"),
            @ApiImplicitParam(name = "keyword",value = "品牌名",required = false,paramType = "String")
    })
    @ApiOperation(value = "获取品牌列表")
    public R getBrandList(@PathVariable Integer page,@PathVariable Integer pageSize,String keyword){
        Map<String,Object> map = brandService.getBrandList(page,pageSize,keyword);
        return R.ok().data(map);
    }

    @PutMapping("/update/factoryStatus")
    @ApiOperation(value = "是否为品牌制造商")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "品牌Id",required = true,paramType = "Long"),
            @ApiImplicitParam(name = "factoryStatus",value = "品牌制造商状态",required = true,paramType = "Integer")
    })
    public R factoryStatus(Long id, Integer factoryStatus){
        int row = brandService.updateFactoryStatus(id,factoryStatus);
        if(row>0){
            return R.ok();
        }else{
            return R.error();
        }
    }

    @PutMapping("/update/showStatus")
    @ApiOperation(value = "是否显示")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "品牌Id",required = true,paramType = "Long"),
            @ApiImplicitParam(name = "showStatus",value = "显示状态",required = true,paramType = "Integer")
    })
    public R showStatus(Long id, Integer showStatus){
        int row = brandService.updateShowStatus(id,showStatus);
        if(row>0){
            return R.ok();
        }else{
            return R.error();
        }
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id获取品牌信息")
    @ApiImplicitParam(name = "id",value = "品牌Id",required = true,paramType = "Long")
    public R getBrandById(@PathVariable Long id){
        Brand brand = brandService.getById(id);
        return R.ok().data("brand",brand);
    }

    @PutMapping("/update")
    @ApiOperation(value = "品牌编辑")
    @ApiImplicitParam(name = "brand",value = "品牌信息",required = true,paramType = "Brand")
    public R update(@RequestBody  Brand brand){
        int row = brandService.updateBrand(brand);
        if(row>0){
            return R.ok();
        }else{
            return R.error();
        }
    }

    @PostMapping("/add")
    @ApiOperation(value = "品牌添加")
    @ApiImplicitParam(name = "brand",value = "品牌信息",required = true,paramType = "Brand")
    public R addBrand(@RequestBody  Brand brand){
        Boolean row = brandService.save(brand);
        if(row){
            return R.ok();
        }else{
            return R.error();
        }
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "品牌删除")
    @ApiImplicitParam(name = "id",value = "品牌Id",required = true,paramType = "Long")
    public R delete(@PathVariable Long id){
        Boolean row = brandService.removeById(id);
        if(row){
            return R.ok().message("删除成功");
        }else{
            return R.error().message("删除失败");
        }
    }
}
